/**
 *机构js 
 **/

/**
 * 添加机构
 **/
function saveOrgan(data){
	db.transaction(function (tx) {
    	tx.executeSql("INSERT INTO ORGAN (ID,NAME,SORT,STATUS,CODE,PID,XZQHID,ADDRESS,ZIPCODE,CONTACTOR,TELEPHONE,ACCOUNT,BANK,GOVERNMENT,RECONSIDERATIONORGAN) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
    			[data.id== undefined ? '':data.id,data.name== undefined ? '':data.name,data.sort== undefined ? '':data.sort,
    			 data.status== undefined ? '':data.status,data.code== undefined ? '':data.code,data.pid== undefined ? '':data.pid,data.xzqhId== undefined ? '':data.xzqhId,
    			 data.address == undefined ? '':data.address,data.zipCode == undefined? '':data.zipCode,
    			 data.contactor == undefined ? '':data.contactor,data.telephone == undefined? '':data.telephone,
    			 data.account== undefined ? '':data.account,data.bank== undefined ? '':data.bank,data.government== undefined ? '':data.government,data.reconsiderationOrgan== undefined ? '':data.reconsiderationOrgan],
       function (tx, rs) { 
       },
	   function (tx, error) {
    	   message("添加机构数据失败");
	   });
    });
}

/**
 * 通过ID获取机构
 **/
function findOrganById(id,callback){
	db.transaction(function (tx) { 
		tx.executeSql('SELECT * FROM ORGAN WHERE ID=?', [id], 
		function (tx, results){ 
			var len = results.rows.length, i;
		    var data = {};
		    for (i = 0; i < len; i++){
		    	data.id = results.rows.item(i).ID;
		    	data.name = results.rows.item(i).NAME;
		    	data.sort = results.rows.item(i).SORT;
		    	data.status = results.rows.item(i).STATUS;
		    	data.code = results.rows.item(i).CODE;
		    	data.pid = results.rows.item(i).PID;
		    	data.xzqhId = results.rows.item(i).XZQHID;
		    	data.address = results.rows.item(i).ADDRESS;
		    	data.zipCode = results.rows.item(i).ZIPCODE;
		    	data.contactor = results.rows.item(i).CONTACTOR;
		    	data.telephone = results.rows.item(i).TELEPHONE;
		    	data.account = results.rows.item(i).ACCOUNT;
		    	data.bank = results.rows.item(i).BANK;
		    	data.government = results.rows.item(i).GOVERNMENT;
		    	data.reconsiderationOrgan = results.rows.item(i).RECONSIDERATIONORGAN;
		    	data.isQuery = results.rows.item(i).ISQUERY;
		    }
			if (typeof(callback) == 'function'){
				callback(data);
			}
	    }, 
	    function(tx, ex) {
            message("获取机构失败");
        });  
	});
}

/**
 * 初始化数据
 **/
function organ(callback,data){
	db.transaction(function (tx) { 
		tx.executeSql('DELETE FROM ORGAN', [], 
		function (tx, results){
			if (typeof(callback) == 'function'){
				callback(data);
			}
		},
		function (tx, ex){
			message("删除机构数据失败");
		});  
	});
}


/**
 * 从服务器上获取机构所有数据.
 **/
function getOrganData(){
	$.ajax({
		type : "post",
		url : getContextPath() + "/organ/findAll",
		dataType : "json",
		error : function(request) {
			message("获取机构列表数据失败");
		},
		success : function(data) {
			$.each(data, function(i, item) {
				/**
				 * 添加到本地
				 **/
				saveOrgan(item);
			});
			message("获取机构列表数据成功");
		}
	});
}

/**
 * 初始化机构数据
 * @param db
 * @param callback
 **/
function initOrgan(callback,data){
	deleteOrganAll(callback,data);
}